/*
 * @Author: ipk518 18163976442@163.com
 * @Date: 2023-08-15 11:36:30
 * @LastEditors: ipk518 18163976442@163.com
 * @LastEditTime: 2023-08-19 17:25:19
 * @FilePath: /WingsUpdater-main/src/PackageResolver/PackageResolverTcp.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include "PackageResolverTcp.h"
#include "IDataTransmission.h"
#include "WingsTcpUdpProto.h"
#include "myLog.h"


using namespace std;

PackageResolverTcp::PackageResolverTcp() 
    :m_tcpHandle(IDataTransmission::create(DATA_TRANSMISSION_TCP))
{
    m_type = "tcp";
}

Result PackageResolverTcp::init() 
{
    qlog_d("package resolver(%s) init.", m_type.c_str());

    return m_tcpHandle->init();
}

Result PackageResolverTcp::resolve(string &aliasReceiver, vector<uint8_t> &dataReceiver)
{
    if (false == m_tcpHandle->wait(-1)) 
    {
        return Result::create(ErrorKind::Timeout);
    }

    // recv
    auto tcpPackage = reinterpret_cast<WingsInternalWrapper *>(m_tcpHandle->take());

    aliasReceiver = std::move(tcpPackage->name);
    dataReceiver = std::move(tcpPackage->byteArray);
    m_tcpHandle->release(tcpPackage);
    tcpPackage = nullptr;

    return Result();
}
